﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Data;
using System.Collections;
using System.Web.UI.WebControls;
using DQYT.HTDZ.CommonFactory;
using DQYT.HTDZ.ISinglewellEvaluationService;
using DQYT.HTDZ.SinglewellEvaluationService;
using DQYT.HTDZ.Model;
using Maticsoft.DBUtility;

namespace DQYT.HTDZ.Web.Views.Analysis
{
    /// <summary>
    /// 响应前台请求，处理单井评价的业务
    /// </summary>
    public class ajax_balance : IHttpHandler
    {
        private IEvaluationService evaluationService;//评价项具体类
        private Dictionary<string, string> SearchDictionary = new Dictionary<string, string>();//存储检索数据的检索条件，<wellid,jcrq>
        private Dictionary<string, EvaluationModel> EvaluationDictionary = new Dictionary<string, EvaluationModel>();//存储EvaluationModel序列
        private Dictionary<string, string> SchemeDictionary = new Dictionary<string, string>();//保存给方案类传的参数列表
        private DataTable evaTb;//存储评价基础数据，如HYGGL
        private EvaluationModel evaModel;//保存评价项评价结果的Model
        private double evaValue;//分数
        /// <summary>
        /// 响应前台请求，调用不同方法处理不同业务
        /// </summary>
        /// <param name="context"></param>
        public void ProcessRequest(HttpContext context)
        {
            string task = context.Request.Params["task"].ToString(); //任务名，getEval_NAME,getEvalMODEL
            DataSet ds = new DataSet();
            if (task.Equals("getEval_NAME"))
            {//获得当前方案下对应的评价项信息
                string schemeid = context.Request.Params["id"].ToString(); //方案id
                ds = getEVAL_DATA(schemeid);
            }
            else if (task.Equals("getEvalMODEL"))
            { //获得当前井号、检测日期和当前方案下各个评价项的分数、建议和总分和总建议
                string wellid = context.Request.Params["wellid"].ToString();
                string jcrq = context.Request.Params["jcrq"].ToString();
                string  schemeid = context.Request.Params["schemeid"].ToString(); //方案id
                ds = getEvalMODEL(wellid, jcrq, schemeid);
            }
            context.Response.ContentType = "application/xml";
            context.Response.Write(ds.GetXml());
            context.Response.End();
        }
        /// <summary>
        /// 检索得到当前方案ID下对应的评价项信息
        /// </summary>
        /// <param name="ID"></param>
        /// <param name="drawCan"></param>
        /// <returns></returns>
        public DataSet getEVAL_DATA(string schemeid)
        {
            //检索当前方案下对应的所有评价项名称
            DataTable EVALIDTb = DbHelperOra.Query_Table("select EVAL_ID,EVAL_NAME from TB_EVALUATION WHERE EVAL_ID IN(select EVAL_ID from TB_SCHEME_EVAL where SCHEME_ID='" + schemeid + "' ) order by EVAL_ID");
            EVALIDTb.TableName = "evatable";
            EVALIDTb.Columns[0].ColumnName = "EVAL_ID";
            EVALIDTb.Columns[1].ColumnName = "EVAL_NAME";
            DataSet ds = new DataSet();
            ds.DataSetName = "evatables";
            ds.Tables.Add(EVALIDTb.Copy());
            return ds;
        }
        /// <summary>
        /// 获得当前井号、检测日期和当前方案下各个评价项的分数、建议和总分和总建议
        /// </summary>
        /// <param name="wellid"></param>
        /// <param name="jcrq"></param>
        /// <param name="schemeid"></param>
        /// <returns></returns>
        public DataSet getEvalMODEL(string wellid, string jcrq, string _schemeid)
        {
            SearchDictionary.Add("wellid", wellid);
            SearchDictionary.Add("jcrq", jcrq);
            decimal schemeid = decimal.Parse(_schemeid);
            //检索当前方案下对应的所有评价项ID
            DataTable EVALIDTb = DbHelperOra.Query_Table("select EVAL_ID from TB_SCHEME_EVAL where SCHEME_ID='" + schemeid + "'order by EVAL_ID");
            //循环计算各个评价项的分数和得出对应的评价结论
            for (int i = 0; i < EVALIDTb.Rows.Count; i++)
            {
                decimal id = (decimal)EVALIDTb.Rows[i][0];
                DataTable EVALFUNCTb = DbHelperOra.Query_Table("select EVAL_FUNC from TB_EVALUATION where EVAL_ID='" + id + "'order by EVAL_ID");
                string funstr = EVALFUNCTb.Rows[0][0].ToString();//实例化对象的路径
                //实例化评价项对象
                string AssemblyPath = funstr.Substring(0, funstr.IndexOf(","));
                string ClassNamespace = funstr.Substring(funstr.IndexOf(",") + 1);
                evaluationService = CompositionFactory.CreateObject<IEvaluationService>(AssemblyPath, ClassNamespace);
                //具体计算评价项分数和得出建议
                evaTb = evaluationService.Gets(SearchDictionary);
                evaValue = evaluationService.Calculator(evaTb);
                evaModel = evaluationService.Evaluate(evaValue);
                evaModel.EvaluationName = evaluationService.EvaluationName;
                EvaluationDictionary.Add(evaModel.EvaluationName, evaModel);
                //添加评价项ID和分数
                SchemeDictionary.Add(id.ToString(), evaValue.ToString());

            }
            //计算总分和总建议
            StandardSchemeService SchemeService = new StandardSchemeService();
            SchemeDictionary.Add("schemeid", _schemeid);//保存方案ID
            //计算出总分和得出总建议
            evaModel = SchemeService.SchemeEvaluate(SchemeDictionary);
            EvaluationDictionary.Add(evaModel.EvaluationName, evaModel);
            //将分数和建议添加到datatable中，最后一项是总分和总建议
            DataTable evalmodelTb = new DataTable();
            evalmodelTb.Columns.Add("EVAL_ID", typeof(System.String));//评价项ID
            evalmodelTb.Columns.Add("EVAL_NAME", typeof(System.String));//评价项名称
            evalmodelTb.Columns.Add("EVAL_VALUE", typeof(System.Double));//评价项分数
            evalmodelTb.Columns.Add("EVAL_DESC", typeof(System.String));//评价项建议
            //循环添加
            foreach (var item in EvaluationDictionary)
            {
                DataRow row = evalmodelTb.NewRow();
                row["EVAL_NAME"] = ((EvaluationModel)item.Value).EvaluationName;
                row["EVAL_VALUE"] = ((EvaluationModel)item.Value).EvaluationValue;
                row["EVAL_DESC"] = ((EvaluationModel)item.Value).EvaluationDesc;
                DataTable ID_TB = DbHelperOra.Query_Table("select EVAL_ID from TB_EVALUATION where EVAL_NAME='" + row["EVAL_NAME"] + "'");
                if (ID_TB.Rows.Count > 0)
                {
                    row["EVAL_ID"] = ID_TB.Rows[0][0];
                }
                evalmodelTb.Rows.Add(row);
                  
            }
            evalmodelTb.TableName = "evalModelTable";
            DataSet ds = new DataSet();
            ds.DataSetName = "evatables";
            ds.Tables.Add(evalmodelTb);
            return ds;

        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}